package org.grow.leave.service;

import org.grow.leave.dao.PermissionMapper;
import org.grow.leave.entity.RoleNotice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

/**
 * @Author: xwg
 * @CreateDate: 2021/11/1
 */

@Service
public class LeavePermissionData implements FilterInvocationSecurityMetadataSource {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
        FilterInvocation filterInvocation = (FilterInvocation) o;
        HttpServletRequest request = filterInvocation.getRequest();
        String shortUrl = request.getRequestURI();
        String method = request.getMethod().toUpperCase();
        List<RoleNotice> roles = permissionMapper.findRolesByRequest(shortUrl, method);
        System.out.println("从微服务私有数据库根据url查询到的角色信息： "+shortUrl+" == "+roles);;
        List r = roles;
        return r;
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return false;
    }
}
